package icu.makka.yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.makka.yygh.common.utils.MD5;
import icu.makka.yygh.model.hosp.HospitalSet;
import icu.makka.yygh.result.Result;
import icu.makka.yygh.service.HospitalSetService;
import icu.makka.yygh.service.impl.HospitalSetServiceImpl;
import icu.makka.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Slf4j
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetServiceImpl hospitalSetService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation("获取所有医院设置信息")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAll() {
        //调用service方法
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Result.ok(hospitalSetList);
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @ApiOperation("逻辑删除医院设置")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable("id") Long id) {
        //调用service方法
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 条件查询带分页
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation("条件查询带分页")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable("current") Integer current,
                                  @PathVariable("limit") Integer limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        log.info("条件查询带分页,current={},limit={},hospitalSetQueryVo={}",current, limit, hospitalSetQueryVo);
        //创建分页对象，传递起始页，每页记录数
        Page<HospitalSet> pageInfo = new Page<>(current, limit);
        //构造条件
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(hospitalSetQueryVo.getHosname() != null, HospitalSet::getHosname, hospitalSetQueryVo.getHosname())
                    .like(hospitalSetQueryVo.getHoscode() != null, HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());

        //调用方法实现分页查询
        hospitalSetService.page(pageInfo, queryWrapper);

        //返回结果
        return Result.ok(pageInfo);
    }

    /**
     * 添加医院设置
     * @param hospitalSet
     * @return
     */
    @ApiOperation("添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        log.info("添加医院设置");
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名密钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(100)));
        //调用service层
        boolean flag = hospitalSetService.save(hospitalSet);
        //返回结果
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据id获取医院设置
     * @param id
     * @return
     */
    @ApiOperation("根据id获取医院设置")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable("id") Long id) {
        log.info("根据id获取医院设置,id={}", id);
        //调用service层
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //返回结果
        return Result.ok(hospitalSet);
    }

    /**
     * 修改医院设置
     * @param hospitalSet
     * @return
     */
    @ApiOperation("修改医院设置")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //调用service层
        boolean flag = hospitalSetService.updateById(hospitalSet);
        //返回结果
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 批量删除医院设置
     * @param idList
     * @return
     */
    @ApiOperation("批量修改医院设置")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        //调用service层
        boolean flag = hospitalSetService.removeByIds(idList);
        //返回结果
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 医院设置锁定和解锁
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用service层
        boolean flag = hospitalSetService.updateById(hospitalSet);
        //返回结果
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 发送签名密钥
     * @param id
     * @return
     */
    @ApiOperation("发送签名密钥")
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable("id") Long id) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //获取医院编号和签名密钥
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        //TODO 1.12 发送签名密钥
        return Result.ok();
    }
}
